#!/bin/sh

# This is not pretty, but it is what we need for the moment

mkdir $SAGE_ROOT/devel/sage/sage/words
cp -r src/* $SAGE_ROOT/devel/sage/sage/words

# notify sage of our existance
if [ ! -e $SAGE_ROOT/devel/sage/sage/all.py.bak ]; then
    cp $SAGE_ROOT/devel/sage/sage/all.py $SAGE_ROOT/devel/sage/sage/all.py.bak
fi
if ! grep "from sage.words.all      import *" $SAGE_ROOT/devel/sage/sage/all.py > /dev/null 2>&1; then
    sed "/from copy import copy, deepcopy/i\\
    from sage.words.all      import *\\
    \\
    " $SAGE_ROOT/devel/sage/sage/all.py.bak > $SAGE_ROOT/devel/sage/sage/all.py;
fi
# restore from backup in case of problems
if [ $? -ne 0 ]; then
    mv $SAGE_ROOT/devel/sage/sage/all.py.bak $SAGE_ROOT/devel/sage/sage/all.py
    echo "Problems installing package, modifications have been reversed"
    exit 1;
fi

# register our package with the setup file
if [ ! -e $SAGE_ROOT/devel/sage/setup.py.bak ]; then
    cp $SAGE_ROOT/devel/sage/setup.py $SAGE_ROOT/devel/sage/setup.py.bak;
fi    
if ! grep "'sage.words'," $SAGE_ROOT/devel/sage/setup.py > /dev/null 2>&1; then
    sed "/'sage.crypto',/i\\
    \\                        'sage.words',\\
    \\
    " $SAGE_ROOT/devel/sage/setup.py.bak > $SAGE_ROOT/devel/sage/setup.py;
fi
# restore from backup in case of problems
if [ $? -ne 0 ]; then
    mv $SAGE_ROOT/devel/sage/setup.py.bak $SAGE_ROOT/devel/sage/setup.py
    mv $SAGE_ROOT/devel/sage/sage/all.py.bak $SAGE_ROOT/devel/sage/sage/all.py
    echo "Problems installing package, modifications have been reversed"
    exit 1;
fi
    

# rebuild the branch so that our modifications are used
sage -b
# restore from backup in case of problems
if [ $? -ne 0 ]; then
    mv $SAGE_ROOT/devel/sage/setup.py.bak $SAGE_ROOT/devel/sage/setup.py
    mv $SAGE_ROOT/devel/sage/sage/all.py.bak $SAGE_ROOT/devel/sage/sage/all.py
    echo "Problems installing package, modifications have been reversed"
    exit 1;
fi
